package com.rla.controller;

import java.util.List;
import java.util.Arrays;

import com.alibaba.fastjson.JSONObject;
import com.rla.domain.vo.VipUserAddressVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.rla.common.annotation.RepeatSubmit;
import com.rla.common.annotation.Log;
import com.rla.common.core.controller.BaseController;
import com.rla.common.core.domain.PageQuery;
import com.rla.common.core.domain.R;
import com.rla.common.utils.validate.AddGroup;
import com.rla.common.utils.validate.EditGroup;
import com.rla.common.enums.BusinessType;
import com.rla.common.utils.poi.ExcelUtil;
import com.rla.domain.vo.MallOrderAddressVo;
import com.rla.domain.bo.MallOrderAddressBo;
import com.rla.service.IMallOrderAddressService;
import com.rla.common.core.page.TableDataInfo;

/**
 * 订单地址绑定
 *
 * @author rla
 * @date 2025-08-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/order/address")
public class MallOrderAddressController extends BaseController {

    private final IMallOrderAddressService iMallOrderAddressService;

    /**
     * 查询订单地址绑定列表
     */
    @GetMapping("/list")
    public TableDataInfo<MallOrderAddressVo> list(MallOrderAddressBo bo, PageQuery pageQuery) {
        return iMallOrderAddressService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订单地址绑定列表
     */
    @Log(title = "订单地址绑定", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MallOrderAddressBo bo, HttpServletResponse response) {
        List<MallOrderAddressVo> list = iMallOrderAddressService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单地址绑定", MallOrderAddressVo.class, response);
    }

    /**
     * 获取订单地址绑定详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<MallOrderAddressVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iMallOrderAddressService.queryById(id));
    }

    /**
     * 新增订单地址绑定
     */
    @Log(title = "订单地址绑定", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MallOrderAddressBo bo) {
        return toAjax(iMallOrderAddressService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改订单地址绑定
     */
    @Log(title = "订单地址绑定", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MallOrderAddressBo bo) {
        return toAjax(iMallOrderAddressService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除订单地址绑定
     *
     * @param ids 主键串
     */
    @Log(title = "订单地址绑定", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iMallOrderAddressService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }


    /**
     * 修改订单地址绑定
     */
    @Log(title = "订单地址绑定", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateByOrderId")
    public R<Void> updateByOrderId(@Validated(EditGroup.class) @RequestBody MallOrderAddressBo bo) {
        return toAjax(iMallOrderAddressService.updateByOrderId(bo) ? 1 : 0);
    }


    /**
     * 根据订单id查询
     * @param orderId
     * @return
     */
    @GetMapping("/queryByOrderId/{orderId}")
    public R<MallOrderAddressVo> queryByOrderId(@PathVariable String orderId) {
        return R.ok(iMallOrderAddressService.queryByOrderId(orderId));
    }

    //上门取件
    @PostMapping("/schedulePickup")
    public JSONObject schedulePickup(@RequestParam String orderId) {
        return iMallOrderAddressService.SMQJ(orderId);
    }


    //取消
    @PostMapping("/cancel")
    public JSONObject cancel(@RequestParam String orderId) {
        return iMallOrderAddressService.cancel(orderId);
    }
}
